﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using Common;

public partial class insurance_orderpayment : MemberPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("___EVENTTARGET"))
        {
            case "gopayment":
                Payment();
                break;
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("objectid") != "")
            {
                InitObjectValues();
                PaymentTypeListDataBind();
            }
        }
    }

    #region 初始化订单数据
    void InitObjectValues()
    {
        Model.Insurance.InsurancePolicyBase _obj = BLL.Insurance.InsurancePolicyBase.GetObjById(HttpUtility.UrlDecode(RequestBase.GetQueryString("objectid")));
        decimal price = BLL.Insurance.InsuranceProductItemBase.GetProductItemObjectById(_obj.ProductItemId).SalesPrice;

        if (price * _obj.PersonQuantity > 0)
        {
            lblOrderNumber.Text = _obj.OrderNumber;
            lblProductItemName.Text = _obj.ProductItemName;
            lblProductName.Text = _obj.ProductName;
            lblPrice.Text = price.ToString();
            lblTotalAmount.Text = (price * _obj.PersonQuantity + _obj.InvoiceFee).ToString();
            lblPersonQuantity.Text = _obj.PersonQuantity.ToString();

            txtObjectId.Value = _obj.InsurancePolicyId;
            txtProductName.Value = "申根旅行险--" + _obj.ProductName + _obj.ProductItemName + "天";
            txtOrderNumber.Value = _obj.OrderNumber;
            txtPrice.Value = price.ToString();
            txtTotalAmount.Value = (price * _obj.PersonQuantity + _obj.InvoiceFee).ToString();
            txtPersonQuantity.Value = _obj.PersonQuantity.ToString();
            txtInvoiceFee.Value = _obj.InvoiceFee.ToString();
            lblInvoiceFee.Text = _obj.InvoiceFee.ToString();
        }
        else
        { 
            divErrorMsg.Style.Add("display", "");
            divErrorMsg.InnerHtml = string.Format(divErrorMsg.InnerHtml, "<li style=\"background: none;\">数据错误，请联系客服协助处理。</li>");
        }
    }

    void PaymentTypeListDataBind()
    {
        DataTable dat = BLL.Search.CommonSearch.ExecSelect("PaymentTypeBase", null, null, null, new string[] { "PaymentTypeId" }, new string[] { "ASC" });

        DataView dv1 = dat.DefaultView;
        dv1.RowFilter = "TypeCode = 1";

        rptPaymentType_1.DataSource = dv1;
        rptPaymentType_1.DataBind();

        DataView dv2 = dat.DefaultView;
        dv2.RowFilter = "TypeCode = 2";

        rptPaymentType_2.DataSource = dv2;
        rptPaymentType_2.DataBind();
    }
    #endregion

    #region 支付

    void Payment()
    {
        string out_trade_no = txtOrderNumber.Value.Trim();
        string subject = txtProductName.Value.Trim();
        string body = "保险计划：" + subject + "，订单编号：" + out_trade_no;
        string total_fee = txtTotalAmount.Value.Trim();
        string paymethod = txtPayMethod.Value.Trim();
        string defaultbank = txtBankCode.Value.Trim();
        string show_url = "http://www.mapfre-assistance.com.cn";

        //把请求参数打包成数组
        SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
        sParaTemp.Add("payment_type", "1");
        sParaTemp.Add("show_url", show_url);
        sParaTemp.Add("out_trade_no", out_trade_no);
        sParaTemp.Add("subject", subject);
        sParaTemp.Add("body", body);
        sParaTemp.Add("total_fee", total_fee);
        sParaTemp.Add("paymethod", paymethod);
        sParaTemp.Add("defaultbank", defaultbank);
        sParaTemp.Add("anti_phishing_key", "");
        sParaTemp.Add("exter_invoke_ip", "");
        sParaTemp.Add("extra_common_param", "");
        sParaTemp.Add("buyer_email", "");
        sParaTemp.Add("royalty_type", "");
        sParaTemp.Add("royalty_parameters", "");

        //构造即时到帐接口表单提交HTML数据，无需修改
        AlipayService ali = new AlipayService();
        string sHtmlText = ali.Create_direct_pay_by_user(sParaTemp);
        Response.Write(sHtmlText);
    }
    #endregion
}